package auction.model;

import java.io.Serializable;
import java.util.Date;


public class CategoryItem2OneToManyEntity{
	
	private Id id=new Id();
	private String username;
	private Date dateAdded=new Date();
	
	private Item2OneToManyEntity item;
	private Category2OneToManyEntity category;
	
	public CategoryItem2OneToManyEntity(){
		
	}
	
	public Id getId(){
		return id;
	}
	
	public void setId(Id i){
		this.id=i;
	}
	
	public CategoryItem2OneToManyEntity(String uname,
											Category2OneToManyEntity category,
											Item2OneToManyEntity item){
		//Set fields
		this.username=uname;
		this.category=category;
		this.item=item;
		
		//Set identifier
		this.id.categoryId=category.getId();
		this.id.itemId=item.getId();
		
		//make the association
		category.getCategorizedItems().add(this);
		item.getCategorizedItems().add(this);
		
	}
	
	
	public static class Id implements Serializable{
		private Long categoryId;
		private Long itemId;
		
		public Id(){
			
		}
		
		public Id(Long cid,Long iid){
			this.categoryId=cid;
			this.itemId=iid;
		}
		
		public boolean equals(Object o){
			if(o != null && o instanceof Id){
				Id that=(Id)o;
				return this.categoryId.equals(that.categoryId) &&
						this.itemId.equals(that.itemId);
			}else{
				return false;
			}
		}
		
		public int hashCode(){
			return categoryId.hashCode()+itemId.hashCode();
		}
	}


	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public Date getDateAdded() {
		return dateAdded;
	}

	public void setDateAdded(Date dateAdded) {
		this.dateAdded = dateAdded;
	}

	public Item2OneToManyEntity getItem() {
		return item;
	}

	public void setItem(Item2OneToManyEntity item) {
		this.item = item;
	}

	public Category2OneToManyEntity getCategory() {
		return category;
	}

	public void setCategory(Category2OneToManyEntity category) {
		this.category = category;
	}
	
}